﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using SysConf;

namespace Park
{
    public partial class SystemConfig : Form
    {
        public SystemConfig()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SystemConfig_Load(object sender, EventArgs e)
        {
            this.SetBounds((Screen.GetBounds(this).Width / 2) - (this.Width / 2), (Screen.GetBounds(this).Height / 2) - (this.Height / 2), this.Width, this.Height, BoundsSpecified.Location);
            WinAPI winAPI = new WinAPI(System.Environment.CurrentDirectory + "/system.ini");

            // 设备信息
            DbIP.Text = winAPI.ReadValue("DeviceConf", "DbIP");
            DbLoginName.Text = winAPI.ReadValue("DeviceConf", "DbLoginName");
            DbPassword.Text = winAPI.ReadValue("DeviceConf", "DbPassword");

            InCameraIP.Text = winAPI.ReadValue("DeviceConf", "InCameraIP");
            InCameraPort.Text = winAPI.ReadValue("DeviceConf", "InCameraPort");
            InCameraLoginName.Text = winAPI.ReadValue("DeviceConf", "InCameraLoginName");
            InCameraPassword.Text = winAPI.ReadValue("DeviceConf", "InCameraPassword");
            OutCameraIP.Text = winAPI.ReadValue("DeviceConf", "OutCameraIP");
            OutCameraPort.Text = winAPI.ReadValue("DeviceConf", "OutCameraPort");
            OutCameraLoginName.Text = winAPI.ReadValue("DeviceConf", "OutCameraLoginName");
            OutCameraPassword.Text = winAPI.ReadValue("DeviceConf", "OutCameraPassword");

            PassPlace.Text = winAPI.ReadValue("DeviceConf", "PassPlace");
            if (winAPI.ReadValue("DeviceConf", "IsPassInVoice") == true.ToString())
            {
                IsPassInVoice.Checked = true;
                PassInVoiceText.Enabled = true;
                PlateLabelIn.Text = "选中即播放车牌";
            }
            PassInVoiceText.Text = winAPI.ReadValue("DeviceConf", "PassInVoiceText");

            if (winAPI.ReadValue("DeviceConf", "IsPassOutVoice") == true.ToString())
            {
                IsPassOutVoice.Checked = true;
                PassOutVoiceText.Enabled = true;
                PlateLabelOut.Text = "选中即播放车牌";
            }
            PassOutVoiceText.Text = winAPI.ReadValue("DeviceConf", "PassOutVoiceText");


            InPicturePath.Text = winAPI.ReadValue("FilePath", "InPicturePath");
            OutPicturePath.Text = winAPI.ReadValue("FilePath", "OutPicturePath");
            WarnFilePath.Text = winAPI.ReadValue("FilePath", "WarnFilePath");

            if (winAPI.ReadValue("FilePath", "InPicturePath") != "")
            {
                InPicturePath.Enabled = false;
                button3.Enabled = false;
            }
            if (winAPI.ReadValue("FilePath", "OutPicturePath") != "")
            {
                OutPicturePath.Enabled = false;
                button4.Enabled = false;
            }
            if (winAPI.ReadValue("FilePath", "WarnFilePath") != "")
            {
                WarnFilePath.Enabled = false;
                button5.Enabled = false;
            }


            //继电器与网络接口
            RelayIP.Text = winAPI.ReadValue("RelayConf", "RelayIP");
            RelayPort.Text = winAPI.ReadValue("RelayConf", "RelayPort");
            GateStatus.Text = winAPI.ReadValue("RelayConf", "GateStatus");
            InInductionCoilStatus.Text = winAPI.ReadValue("RelayConf", "InInductionCoilStatus");
            OutInductionCoilStatus.Text = winAPI.ReadValue("RelayConf", "OutInductionCoilStatus");
            GateSwitch.Text = winAPI.ReadValue("RelayConf", "GateSwitch");
            WarnSwitch.Text = winAPI.ReadValue("RelayConf", "WarnSwitch");
            InLightSwitch.Text = winAPI.ReadValue("RelayConf", "InLightSwitch");
            OutLightSwitch.Text = winAPI.ReadValue("RelayConf", "OutLightSwitch");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (validaing(DbIP, "数据库IP地址") == false) { return; }
            if (validaing(DbLoginName, "数据库用户名") == false) { return; }
            if (validaing(DbPassword, "数据库密码") == false) { return; }

            if (validaing(InCameraIP, "入口摄像头IP地址") == false) { return; }
            if (validaing(InCameraPort, "入口摄像头端口") == false) { return; }
            if (validaing(InCameraLoginName, "入口摄像头登录名") == false) { return; }
            if (validaing(InCameraPassword, "入口摄像头登录密码") == false) { return; }
            if (validaing(OutCameraIP, "出口摄像头IP地址") == false) { return; }
            if (validaing(OutCameraPort, "出口摄像头端口") == false) { return; }
            if (validaing(OutCameraLoginName, "出口摄像头登录名") == false) { return; }
            if (validaing(OutCameraPassword, "出口摄像头登录密码") == false) { return; }
            if (validaing(PassPlace, "出入口位置") == false) { return; }

            if (validaing(RelayIP, "继电器IP地址") == false) { return; }
            if (validaing(RelayPort, "继电器端口") == false) { return; }
            if (validaing(GateStatus, "道闸开关状态信号") == false) { return; }
            if (validaing(InInductionCoilStatus, "入口地感线圈状态信号") == false) { return; }
            if (validaing(OutInductionCoilStatus, "出口地感线圈状态信号") == false) { return; }
            if (validaing(GateSwitch, "道闸开关控制信号") == false) { return; }
            if (validaing(WarnSwitch, "报警器开关控制信号") == false) { return; }
            if (validaing(InLightSwitch, "入口信号灯开关信号") == false) { return; }
            if (validaing(OutLightSwitch, "出口信号灯开关信号") == false) { return; }

            if (validaing(InPicturePath, "驶入图片地址") == false) { return; }
            if (validaing(OutPicturePath, "驶出图片地址") == false) { return; }
            if (validaing(WarnFilePath, "报警声音文件") == false) { return; }


            WinAPI winAPI = new WinAPI(System.Environment.CurrentDirectory + "/system.ini");

            // 设备信息
            winAPI.Write("DeviceConf", "DbIP", DbIP.Text);
            winAPI.Write("DeviceConf", "DbLoginName", DbLoginName.Text);
            winAPI.Write("DeviceConf", "DbPassword", DbPassword.Text);
            winAPI.Write("DeviceConf", "InCameraIP", InCameraIP.Text);
            winAPI.Write("DeviceConf", "InCameraPort", InCameraPort.Text);
            winAPI.Write("DeviceConf", "InCameraLoginName", InCameraLoginName.Text);
            winAPI.Write("DeviceConf", "InCameraPassword", InCameraPassword.Text);
            winAPI.Write("DeviceConf", "OutCameraIP", OutCameraIP.Text);
            winAPI.Write("DeviceConf", "OutCameraPort", OutCameraPort.Text);
            winAPI.Write("DeviceConf", "OutCameraLoginName", OutCameraLoginName.Text);
            winAPI.Write("DeviceConf", "OutCameraPassword", OutCameraPassword.Text);
            winAPI.Write("DeviceConf", "PassPlace", PassPlace.Text);
            winAPI.Write("DeviceConf", "IsPassInVoice", IsPassInVoice.Checked.ToString());
            winAPI.Write("DeviceConf", "PassInVoiceText", PassInVoiceText.Text);
            winAPI.Write("DeviceConf", "IsPassOutVoice", IsPassOutVoice.Checked.ToString());
            winAPI.Write("DeviceConf", "PassOutVoiceText", PassOutVoiceText.Text);

            //继电器与网络接口
            winAPI.Write("RelayConf", "RelayIP", RelayIP.Text);
            winAPI.Write("RelayConf", "RelayPort", RelayPort.Text);
            winAPI.Write("RelayConf", "GateStatus", GateStatus.Text);
            winAPI.Write("RelayConf", "InInductionCoilStatus", InInductionCoilStatus.Text);
            winAPI.Write("RelayConf", "OutInductionCoilStatus", OutInductionCoilStatus.Text);
            winAPI.Write("RelayConf", "GateSwitch", GateSwitch.Text);
            winAPI.Write("RelayConf", "WarnSwitch", WarnSwitch.Text);
            winAPI.Write("RelayConf", "InLightSwitch", InLightSwitch.Text);
            winAPI.Write("RelayConf", "OutLightSwitch", OutLightSwitch.Text);

            //图片保存地址
            winAPI.Write("FilePath", "InPicturePath", InPicturePath.Text);
            winAPI.Write("FilePath", "OutPicturePath", OutPicturePath.Text);
            winAPI.Write("FilePath", "WarnFilePath", WarnFilePath.Text);

            MessageBox.Show("系统配置信息已设置！请重新运行监控程序。");
            this.Close();
        }

        private void IsPassInVoice_CheckedChanged(object sender, EventArgs e)
        {
            if (IsPassInVoice.Checked)
            {
                PassInVoiceText.Enabled = true;
                PlateLabelIn.Text = "选中即播放车牌";
            }
            else
            {
                PassInVoiceText.Enabled = false;
                PlateLabelIn.Text = "";
            }
        }

        private void IsPassOutVoice_CheckedChanged(object sender, EventArgs e)
        {
            if (IsPassOutVoice.Checked)
            {
                PassOutVoiceText.Enabled = true;
                PlateLabelOut.Text = "选中即播放车牌";
            }
            else
            {
                PassOutVoiceText.Enabled = false;
                PlateLabelOut.Text = "";
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (InPictureDialog.ShowDialog() == DialogResult.OK)
            {
                InPicturePath.Text = InPictureDialog.SelectedPath;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (OutPictureDialog.ShowDialog() == DialogResult.OK)
            {
                OutPicturePath.Text = OutPictureDialog.SelectedPath;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //WarnFileDialog.Filter = "Windows media file (*.avi;*.mpeg;*.mpg;*.wav)|*.avi;*.mpeg;*.mpg|All (*.*)|*.*";
            if (WarnFileDialog.ShowDialog() == DialogResult.OK)
            {
                WarnFilePath.Text = WarnFileDialog.FileName;
            }
        }


        /// 验证输入框的输入内容
        /// </summary>
        /// <param name="textbox">输入框控件</param>
        /// <param name="controlName">输入框的名称</param>
        /// <returns>true验证通过，false验证不通过</returns>
        private bool validaing(TextBox textbox, string controlName)
        {
            bool result = false;
            if (textbox.TextLength == 0)
            {
                errorProvider.SetError(textbox, "请输入" + controlName + "！");
                textbox.SelectAll();
                textbox.Focus();

            }
            else
            {
                errorProvider.SetError(textbox, "");
                result = true;
            }
            return result;

        }
    }
}
